﻿using BarTender;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Linq;

namespace wms_wpf.Extend
{
    public static class BarTenderPrint
    {

       
            public static BarTender.Application btApp = new BarTender.Application();
            /// <summary>
            /// BarTender运行软件
            /// </summary>
            public static BarTender.Format btFormat = new BarTender.Format();
            /// <summary>
            /// 打印
            /// </summary>
            /// <param name="printerName">打印机名称</param>
            /// <param name="btFileName">打印文件</param>
            /// <param name="dict">内容字典</param>
            /// <param name="CopiesOfLabel">打印数量</param>
            public static bool Print(string printerName, string btFileName, Dictionary<string, string> dict, int CopiesOfLabel)
            {
                try
                {
                    var btApp = new BarTender.Application();
                    btFormat = btApp.Formats.Open(btFileName);
                   btFormat.PrintSetup.Printer = printerName;
                   foreach (var item in dict)
                    {
                        btFormat.SetNamedSubStringValue(item.Key, item.Value);
                    }//加了内容字典就打不出来了
                    btFormat.PrintSetup.IdenticalCopiesOfLabel = CopiesOfLabel;

                    btFormat.PrintOut(false, false);


                btFormat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
                Marshal.ReleaseComObject(btFormat);
                btApp.Quit(BarTender.BtSaveOptions.btSaveChanges);
                Marshal.ReleaseComObject(btApp);
                GC.Collect(); // 强制垃圾回收
                GC.WaitForPendingFinalizers(); // 等待终结器完成
                return true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("【打印机" + printerName + "】", ex.Message);
                    return false;
                }

            }
            public static void Label(string Lpn,string MaterialName,string MaterialTitle,string order,string production,string batch,string quantity,string datetime )
           {
            bool Falg = false;
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "文档1.btw";
            if (string.IsNullOrEmpty(Lpn))
            {
                return;
            }
            string LPN = Lpn;
            string 物料编码 = MaterialName;
            string 物料名称 = MaterialTitle;
            string 工单编码 = order;
            string 工序编码 = production;
            string 生产批次 = batch;
            string 数量 = quantity;
            string 出库日期 = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
            string 二维码 = LPN + "&" + 物料编码 + "&" + 出库日期 + "&" + 数量;
            //传递打印内容，Key-Vaule
            Dictionary<string, string> dic = new Dictionary<string, string>()
                     {
                            {"LPN",LPN },
                            {"物料编码",物料编码},
                            {"物料名称",物料名称},
                            {"工单编码",工单编码},
                            {"工序编码",工序编码},
                            {"生产批次",生产批次},
                            {"数量",数量 },
                            {"出库日期",出库日期},
                            {"二维码",二维码}
                     };
            Falg = BarTenderPrint.Print("ZDesigner ZD421CN-300dpi ZPL (副本 1)", filePath, dic, 1);//表示有一个名称为SN的打印机，然后打印模板的路径，打印内容，打印数量
        }
        
    }
}
